home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0111_File Viewer Object for Text Files.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  4.8 KB  |  210 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal 6.0                             }
  4. {   Turbo Vision Demo                            }
  5. {   Copyright (c) 1990 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit FViewer;
  10.  
  11. {$F+,O+,X+,S-,D+}
  12.  
  13. { FileViewer object for scrolling through text files. See
  14.   TVDEMO.PAS for an example program that uses this unit.
  15. }
  16.  
  17. interface
  18.  
  19. uses Objects, Views, Dos;
  20.  
  21. type
  22.  
  23.   { TLineCollection }
  24.  
  25.   PLineCollection = ^TLineCollection;
  26.   TLineCollection = object(TCollection)
  27.     procedure FreeItem(P: Pointer); virtual;
  28.   end;
  29.  
  30.   { TFileViewer }
  31.  
  32.   PFileViewer = ^TFileViewer;
  33.   TFileViewer = object(TScroller)
  34.     FileName: PString;
  35.     FileLines: PCollection;
  36.     IsValid: Boolean;
  37.     constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar;
  38.       var AFileName: PathStr);
  39.     constructor Load(var S: TStream);
  40.     destructor Done; virtual;
  41.     procedure Draw; virtual;
  42.     procedure ReadFile(var FName: PathStr);
  43.     procedure SetState(AState: Word; Enable: Boolean); virtual;
  44.     procedure Store(var S: TStream);
  45.     function Valid(Command: Word): Boolean; virtual;
  46.   end;
  47.  
  48.   { TFileWindow }
  49.  
  50.   PFileWindow = ^TFileWindow;
  51.   TFileWindow = object(TWindow)
  52.     constructor Init(var FileName: PathStr);
  53.   end;
  54.  
  55. const
  56.  
  57.   RFileViewer: TStreamRec = (
  58.      ObjType: 10080;
  59.      VmtLink: Ofs(TypeOf(TFileViewer)^);
  60.      Load:    @TFileViewer.Load;
  61.      Store:   @TFileViewer.Store
  62.   );
  63.   RFileWindow: TStreamRec = (
  64.      ObjType: 10081;
  65.      VmtLink: Ofs(TypeOf(TFileWindow)^);
  66.      Load:    @TFileWindow.Load;
  67.      Store:   @TFileWindow.Store
  68.   );
  69.  
  70. procedure RegisterFViewer;
  71.  
  72. implementation
  73.  
  74. uses Drivers, Memory, MsgBox, App;
  75.  
  76. { TLineCollection }
  77. procedure TLineCollection.FreeItem(P: Pointer);
  78. begin
  79.   DisposeStr(P);
  80. end;
  81.  
  82. { TFileViewer }
  83. constructor TFileViewer.Init(var Bounds: TRect; AHScrollBar,
  84.   AVScrollBar: PScrollBar; var AFileName: PathStr);
  85. begin
  86.   TScroller.Init(Bounds, AHScrollbar, AVScrollBar);
  87.   GrowMode := gfGrowHiX + gfGrowHiY;
  88.   FileName := nil;
  89.   ReadFile(AFileName);
  90. end;
  91.  
  92. constructor TFileViewer.Load(var S: TStream);
  93. var
  94.   FName: PathStr;
  95. begin
  96.   TScroller.Load(S);
  97.   FileName := S.ReadStr;
  98.   FName := FileName^;
  99.   ReadFile(FName);
  100. end;
  101.  
  102. destructor TFileViewer.Done;
  103. begin
  104.   Dispose(FileLines, Done);
  105.   DisposeStr(FileName);            {RJW Mod}
  106.   TScroller.Done;
  107. end;
  108.  
  109. procedure TFileViewer.Draw;
  110. var
  111.   B: TDrawBuffer;
  112.   C: Byte;
  113.   I: Integer;
  114.   S: String;
  115.   P: PString;
  116. begin
  117.   C := GetColor(1);
  118.   for I := 0 to Size.Y - 1 do
  119.   begin
  120.     MoveChar(B, ' ', C, Size.X);
  121.     if Delta.Y + I < FileLines^.Count then
  122.     begin
  123.       P := FileLines^.At(Delta.Y + I);
  124.       if P <> nil then S := Copy(P^, Delta.X + 1, Size.X)
  125.       else S := '';
  126.       MoveStr(B, S, C);
  127.     end;
  128.     WriteLine(0, I, Size.X, 1, B);
  129.   end;
  130. end;
  131.  
  132. procedure TFileViewer.ReadFile(var FName: PathStr);
  133. var
  134.   FileToView: Text;
  135.   Line: String;
  136.   MaxWidth: Integer;
  137.   E: TEvent;
  138. begin
  139.   IsValid := True;
  140.   if FileName <> nil then DisposeStr(FileName);
  141.   FileName := NewStr(FName);
  142.   FileLines := New(PLineCollection, Init(5,5));
  143.   {$I-}
  144.   Assign(FileToView, FName);
  145.   Reset(FileToView);
  146.   if IOResult <> 0 then
  147.   begin
  148.     MessageBox('Cannot open file '+FName+'.', nil, mfError + mfOkButton);
  149.     IsValid := False;
  150.   end
  151.   else
  152.   begin
  153.     MaxWidth := 0;
  154.     while not Eof(FileToView) and not LowMemory do
  155.     begin
  156.       Readln(FileToView, Line);
  157.       if Length(Line) > MaxWidth then MaxWidth := Length(Line);
  158.       FileLines^.Insert(NewStr(Line));
  159.     end;
  160.     Close(FileToView);
  161.   end;
  162.   {$I+}
  163.   Limit.X := MaxWidth;
  164.   Limit.Y := FileLines^.Count;
  165. end;
  166.  
  167. procedure TFileViewer.SetState(AState: Word; Enable: Boolean);
  168. begin
  169.   TScroller.SetState(AState, Enable);
  170.   if Enable and (AState and sfExposed <> 0) then
  171.      SetLimit(Limit.X, Limit.Y);
  172. end;
  173.  
  174. procedure TFileViewer.Store(var S: TStream);
  175. begin
  176.   TScroller.Store(S);
  177.   S.WriteStr(FileName);
  178. end;
  179.  
  180. function TFileViewer.Valid(Command: Word): Boolean;
  181. begin
  182.   Valid := IsValid;
  183. end;
  184.  
  185. { TFileWindow }
  186. constructor TFileWindow.Init(var FileName: PathStr);
  187. const
  188.   WinNumber: Integer = 1;
  189. var
  190.   R: TRect;
  191. begin
  192.   Desktop^.GetExtent(R);
  193.   TWindow.Init(R, Filename, WinNumber);
  194.   Options := Options or ofTileable;
  195.   Inc(WinNumber);
  196.   GetExtent(R);
  197.   R.Grow(-1, -1);
  198.   Insert(New(PFileViewer, Init(R,
  199.     StandardScrollBar(sbHorizontal + sbHandleKeyboard),
  200.     StandardScrollBar(sbVertical + sbHandleKeyboard), Filename)));
  201. end;
  202.  
  203. procedure RegisterFViewer;
  204. begin
  205.   RegisterType(RFileViewer);
  206.   RegisterType(RFileWindow);
  207. end;
  208.  
  209. end.
  210.